﻿using System;
using System.Text;
using System.Runtime.InteropServices;

public struct TCOM_HEAD{
    [MarshalAs(UnmanagedType.LPTStr)]
    public string StaID;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string MsgType;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string sMsgIndex;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string sPsw;
    [MarshalAs(UnmanagedType.LPTStr)]
    public string sAction;
}

/*
public struct MEMORYSTATUS
{
    public int dwLength;
    public int dwMemoryLoad;
    public int dwTotalPhys;
    public int dwAvailPhys;
    public int dwTotalPageFile;
    public int dwAvailPageFile;
    public int dwTotalVirtual;
    public int dwAvailVirtual;
}
*/

namespace DllTest
{
     public class DLL
    {
         /*
         [DllImport("MixIO.dll",CallingConvention = CallingConvention.StdCall)]
            public static extern IntPtr OrderCfg([MarshalAs(UnmanagedType.LPTStr)] string szCfgs,int index);
        
         [DllImport("MixIO.dll")]
            public static extern IntPtr ExplicitMsg(int nFm,[MarshalAs(UnmanagedType.LPTStr)] string szData,
             [MarshalAs(UnmanagedType.LPTStr)] string szCfgs, [MarshalAs(UnmanagedType.LPTStr)] string szTable);
        
         [DllImport("MixIO.dll")]
            public static extern IntPtr ExplicitMsgMultiThread([MarshalAs(UnmanagedType.LPTStr)] string szData,
             [MarshalAs(UnmanagedType.LPTStr)] string szCfgs, [MarshalAs(UnmanagedType.LPTStr)] string szTable,[MarshalAs(UnmanagedType.LPTStr)] string szlpLast);

        
         [DllImport("MixIO.dll")]
//         public static extern void FreeMem([MarshalAs(UnmanagedType.LPTStr)] string szData);
//         public static extern IntPtr FreeMem([MarshalAs(UnmanagedType.LPTStr)] string szData);
            public static extern void FreeMem(IntPtr ptrLast);


//        [DllImport("MixIO.dll")]
//        public static extern IntPtr EnTCOM([MarshalAs(UnmanagedType.LPTStr)] string szStaID, [MarshalAs(UnmanagedType.LPTStr)] string szMsgIndex, 
//            [MarshalAs(UnmanagedType.LPTStr)] string szPsw, [MarshalAs(UnmanagedType.LPTStr)] string szData);
        
         [DllImport("MixIO.dll")]
            public static extern IntPtr EnTCOM(TCOM_HEAD head, [MarshalAs(UnmanagedType.LPTStr)] string szData);
         
         [DllImport("MixIO.dll")]
            public static extern IntPtr ReadVersion();

         [DllImport("MixIO.dll")]
            public static extern IntPtr JsonStyledOut([MarshalAs(UnmanagedType.LPTStr)] string szJsonNormal);
        
         [DllImport("MixIO.dll")]
          public static extern IntPtr ReadCfg([MarshalAs(UnmanagedType.LPTStr)] string szFieldID,
            [MarshalAs(UnmanagedType.LPTStr)] string szCfgs, [MarshalAs(UnmanagedType.LPTStr)] string szTable);
        
         [DllImport("MixIO.dll")]
            public static extern IntPtr GetFieldName([MarshalAs(UnmanagedType.LPTStr)] string szFieldID,
            [MarshalAs(UnmanagedType.LPTStr)] string szTable);
        
         [DllImport("MixIO.dll")]
            public static extern IntPtr WriteCfg([MarshalAs(UnmanagedType.LPTStr)] string szNewValue,
            [MarshalAs(UnmanagedType.LPTStr)] string szFieldID,[MarshalAs(UnmanagedType.LPTStr)] string szCfgs, [MarshalAs(UnmanagedType.LPTStr)] string szTable);

        //LPTSTR EditCfg(LPTSTR lpCfg, int fDoIndex, LPTSTR lpPara);
        
         [DllImport("MixIO.dll")]
            public static extern IntPtr EditCfg([MarshalAs(UnmanagedType.LPTStr)] string szCfg, int fDoIndex, [MarshalAs(UnmanagedType.LPTStr)] string szPara);

         [DllImport("MixIO.dll")]
         public static extern sbyte DoCfgCheck([MarshalAs(UnmanagedType.LPTStr)] string szCfg);
         */

//================================================= K_DLL ========================================================================

         [DllImport("Struct.dll")]
         public static extern IntPtr GetMsgInfo(
             [MarshalAs(UnmanagedType.LPTStr)] string szMsg);

         [DllImport("Struct.dll")]
         public static extern IntPtr ParseMsg(
             int outContent, 
             [MarshalAs(UnmanagedType.LPTStr)] string szMsg, 
             [MarshalAs(UnmanagedType.LPTStr)] string szCfg, 
             [MarshalAs(UnmanagedType.LPTStr)] string szMdl);

         [DllImport("Struct.dll")]
         public static extern IntPtr ParseSeg(
             [MarshalAs(UnmanagedType.LPTStr)] string szMsg);


         [DllImport("Struct.dll")]
         public static extern Boolean CheckPack(
             [MarshalAs(UnmanagedType.LPTStr)] string szFile);


         //ReadDevice(String fmtOut, String jCfg, String jMdl);
         [DllImport("Struct.dll")]
         public static extern IntPtr ReadDevice(
             [MarshalAs(UnmanagedType.LPTStr)] string fmtOut,
             [MarshalAs(UnmanagedType.LPTStr)] string szCfg, 
             [MarshalAs(UnmanagedType.LPTStr)] string szMdl);


         //WriteDevice(String absName, String jVal, String jCfg, String jMdl);
         [DllImport("Struct.dll")]
         public static extern IntPtr WriteDevice(
             [MarshalAs(UnmanagedType.LPTStr)] string szAbsName, 
             [MarshalAs(UnmanagedType.LPTStr)] string szVal,
             [MarshalAs(UnmanagedType.LPTStr)] string szCfg, 
             [MarshalAs(UnmanagedType.LPTStr)] string szMdl);


         //--------------------------------------------------------------------------------------------------------------------//

         public string doGetMsgInfo(string szMsg){
             IntPtr pMsg = GetMsgInfo(szMsg);
             string s = Marshal.PtrToStringAuto(pMsg);
             //FreeMem(pMsg);
             return s;
         }

         public string doParseMsg(int outContent, string szMsg, string szCfg, string szMdl){
             IntPtr pMsg = ParseMsg(outContent, szMsg, szCfg, szMdl);
             string s = Marshal.PtrToStringAuto(pMsg);
             //FreeMem(pMsg);
             return s;
         }

         public string doParseSeg(string szMsg){
             IntPtr pMsg = ParseSeg(szMsg);
             string s = Marshal.PtrToStringAuto(pMsg);
             //FreeMem(pMsg);
             return s;
         }

         public Boolean doCheckPack(string szFile){
             Boolean f = CheckPack(szFile);
             return f;
         }


         public string doReadDevice(string fmtOut, string szCfg, string szMdl){

             IntPtr pDevice = ReadDevice(fmtOut, szCfg, szMdl);
             string jsonDevice = Marshal.PtrToStringAuto(pDevice);
             //FreeMem(pMsg);
             return jsonDevice;
         }


         public string doWriteDevice(string szAbsName, string szVal, string szCfg, string szMdl){
             IntPtr pCfg = WriteDevice(szAbsName, szVal, szCfg, szMdl);
             string sCfg = Marshal.PtrToStringAuto(pCfg);
             //FreeMem(pMsg);
             return sCfg;
         }

//=========================================================================================================================
         /*
         public sbyte CallCheckCfg(string sCfg)
         {
             return DLL.DoCfgCheck(sCfg);
         }
         
         public string CallOrderCfg(string szCfgs, int iOrder)
        {
            return Marshal.PtrToStringAuto(DLL.OrderCfg(szCfgs, iOrder));
        }

//
        public string CallExplicitMsg(int nFm, string szData, string szCfgs, string szTable)
        {
            IntPtr pMsg=ExplicitMsg(nFm, szData, szCfgs, szTable);
            string s = Marshal.PtrToStringAuto(pMsg);
//            FreeMem(pMsg);
            return s;
        }

        

        public string CallExplicitMsgMultiThread(string szData, string szCfgs, string szTable, string szlpLast)
        {
            return Marshal.PtrToStringAuto(DLL.ExplicitMsgMultiThread(szData, szCfgs, szTable, szlpLast));
        }
        public string CallReadVersion()
        {
            return Marshal.PtrToStringAuto(DLL.ReadVersion());
        }
        public string CallJsonStyledOut(string szJsonNormal)
        {
            return Marshal.PtrToStringAuto(DLL.JsonStyledOut(szJsonNormal));
        }


//        public string Call_EnTCOM(string sta_id,string msg_index,string psw,string str)
//        {
//            return Marshal.PtrToStringAuto(DLL.EnTCOM(sta_id, msg_index,psw, str));
//        }
        public string Call_EnTCOM(TCOM_HEAD head, string str)
        {
            return Marshal.PtrToStringAuto(DLL.EnTCOM(head, str));
        }

        public string CallReadCfg(string szFieldID, string szCfgs, string szTable)
        {
            return Marshal.PtrToStringAuto(DLL.ReadCfg(szFieldID, szCfgs, szTable));
        }
        public string CallGetFieldName(string szFieldID, string szTable)
        {
            return Marshal.PtrToStringAuto(DLL.GetFieldName(szFieldID,szTable));
        }
        public string CallWriteCfg(string szNewValue,string szFieldID, string szCfgs, string szTable)
        {
            return Marshal.PtrToStringAuto(DLL.WriteCfg(szNewValue,szFieldID, szCfgs, szTable));
        }
        public string CallEditCfg(string szCfg, int fDoIndex, string szPara)
        {
            return Marshal.PtrToStringAuto(DLL.EditCfg(szCfg, fDoIndex, szPara));
        }*/

/*        public static MEMORYSTATUS GetMemoryInfo()
        {
            MEMORYSTATUS vBuffer = new MEMORYSTATUS();
            GlobalMemoryStatus(ref vBuffer);
            StringBuilder vMessage = new StringBuilder();
            vMessage.Append(string.Format("Total virtual memory ={0}\r\n ", vBuffer.dwTotalVirtual));
            vMessage.Append(string.Format("Virtual memory useage ={0}\r\n ", vBuffer.dwAvailVirtual));
            vMessage.Append(string.Format("Available physical memory ={0}\r\n ", vBuffer.dwAvailPhys));
            vMessage.Append(string.Format("Total physical memory ={0}\r\n ", vBuffer.dwTotalPhys));
            System.Console.WriteLine(vMessage.ToString());
            return vBuffer;
        }*/

     }
}
